20. Quiz: Compound Data Structures

Quiz: Adding Values to Nested Dictionaries

Try your hand at working with nested dictionaries. Add another entry, 'is_noble_gas,' to each dictionary in the elements dictionary. After inserting the new entries you should be able to perform these lookups:

>>> print(elements['hydrogen']['is_noble_gas'])
False
>>> print(elements['helium']['is_noble_gas'])
True

Start Quiz:

elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},
            'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

# todo: Add an 'is_noble_gas' entry to the hydrogen and helium dictionaries
# hint: helium is a noble gas, hydrogen isn't

Collections

When we have a group of data we can think about it as a collection (of data elements). In this lesson, we have seen many different data structures that Python provides for storing, accessing and manipulating collections of data. In particular, we have seen lists, sets, and dictionaries.

In the next few quizzes, you will have a chance to practice and review the properties of lists, sets, and dictionaries.

Comparing Collections, Lists

Check the attributes of a collection for which using a Python list would be appropriate.

SOLUTION:
  • Items are always indexed with numbers starting at 0
  • Sortable
  • Add items with `.append`

Comparing Collections, Sets

Check the attributes of a collection for which using a Python set would be appropriate.

SOLUTION:
  • Order in which items appear can be inconsistent
  • Mutable (you can change it)
  • Add items with `.add`

Comparing Collections, Dictionaries

Check the attributes of a collection for which using a Python dictionary would be appropriate.

SOLUTION:
  • Each item contains two parts
  • Order in which items appear can be inconsistent
  • Can be nested